package com.wuyou.simple;

import java.util.ArrayList;
import java.util.List;

/**
 * 3206. 交替组 I
 *
 * @Date 2024/11/26 10:12
 * @Author 无忧
 */
public class NumberOfAlternatingGroups {


    public static void main(String[] args) {
//        int[][] colors = {{1, 1, 1}, {0, 1, 0, 0, 1}, {0, 0, 1}, {0, 0, 1, 0, 0, 1}, {0,1,0,1,1}};
//        for (int i = 0; i < colors.length; i++) {
//            System.out.println(sol_2(colors[i]));
//        }
        int[][] colors = {{0, 1, 0, 0, 1, 0, 1}, {0, 1, 0, 1, 0}};
        int[] k = {6, 3};
        for (int i = 0; i < colors.length; i++) {
            System.out.println(sol_4(colors[i], k[i]));
        }

    }

    public static List<Integer> sol_5(int[] colors, int[][] queries) {
        List<Integer> ans = new ArrayList<>();
        for(int i=0;i<queries.length;i++){
            if (queries[i][0] == 2) {
                colors[queries[i][1]] = queries[i][2];
            }else {
                int res = 0, l = 0, r = l + 1, count = 1, length = colors.length, count_r = r, k = queries[i][1];
                while (count_r < length + (k - 1)) {
                    if (colors[l] != colors[r]) {
                        count++;
                    } else {
                        count = 1;
                    }
                    l++;
                    r++;
                    count_r++;
                    if (l == length) {
                        l = 0;
                    }
                    if (r == length) {
                        r = 0;
                    }
                    if (count >= k) {
                        res++;
                        count--;
                    }
                }
                ans.add(res);
            }
        }
        return ans;
    }
    /**
     * 取余计算效率低
     * @param colors
     * @param k
     * @return
     */
    public static int sol_4(int[] colors, int k) {
        int res = 0, l = 0, r = l + 1, count = 1, length = colors.length, count_r = r;
        while (count_r < length + (k - 1)) {
            if (colors[l] != colors[r]) {
                count++;
            } else {
                count = 1;
            }
            l++;
            r++;
            count_r++;
            if (l == length) {
                l = 0;
            }
            if (r == length) {
                r = 0;
            }
            if (count >= k) {
                res++;
                count--;
            }
        }
        return res;
    }

    public static int sol_3(int[] colors, int k) {
        int res = 0, l = 0, length = colors.length;
        while (l < length) {
            int r = l + 1, count = 1;
            while (r < length + (k - 1) && colors[l % length] != colors[r % length]) {
                l++;
                r++;
                count++;
            }
            if (count >= k) {
                res += count - k + 1;
            }
            l++;
        }
        return res;
    }

    public static int sol_2(int[] colors) {
        int res = 0, l = 0, length = colors.length;
        while (l < length) {
            int r = l + 1, count = 1;
            while (r < length + 2 && colors[l % length] != colors[r % length]) {
                l++;
                r++;
                count++;
            }
            if (count >= 3) {
                res += count - 3 + 1;
            }
            l++;
        }
        return res;
    }

    public static int sol_1(int[] colors) {
        int res = 0, i = 0, count_i = i;
        while (count_i < colors.length && i < colors.length) {
            int l = i, r = l + 1, r_count = r, count = 1;
            if (r >= colors.length) {
                r = 0;
            }
            while (r_count < colors.length + 2 && colors[l++] != colors[r++]) {
                r_count++;
                count++;
                count_i++;
                if (l == colors.length) {
                    l = 0;
                }
                if (r == colors.length) {
                    r = 0;
                }
            }
            if (count >= 3) {
                res += count - 3 + 1;
            }
            count_i++;
            i = l;
        }
        return res;
    }
}
